export default {
    data() {
      return {
        searchItem: {
          searchKey: "",
        },
        showDevice: false,
        styles: {
          height: "calc(100% - 55px)",
          overflow: "auto",
          paddingBottom: "53px",
          position: "static",
        },
        typeList: [
          {
            key: "10",
            title: "老师",
          },
          {
            key: "0",
            title: "学生",
          },
          {
            key: "1",
            title: "住校生",
          },
          {
            key: "2",
            title: "走读生",
          },
          {
            key: "99",
            title: "其他",
          },
        ],
        columns: [
          {
            type: "selection",
            width: 60,
            align: "center",
            fixed: "left",
          },
          {
            title: "姓名",
            key: "name",
            fixed: "left",
            width: 150,
          },
          {
            title: "用户类型",
            key: "type",
            align: "center",
          },
        ],
        list: [],
        noList: [],
        count: 0,
        total: 0,
        page: 1,
        pageSize: 10,
        datalist: [],
  
        groupId: "",
        loading: false,
        userList: [],
      };
    },
    computed: {
      user() {
        return this.$store.state.user;
      },
    },
    methods: {
      selectMenu(name) {
        this.groupId = name;
        this.getUser();
      },
      addAction() {
        this.$axios({
          url: `/group/device/group/notin/${this.groupId}`,
          method: "GET",
        }).then((res) => {
          if (res.code == 200) {
            this.noList = res.data;
            this.deviceList = [];
            this.showDevice = true;
          } else {
            this.$Message.error(res.msg);
          }
        });
      },
      selectDevices(users) {
        this.userList = users;
      },
  
      removeAction() {
        if (this.userList.length == 0) {
          this.$Message.error("请先选择需要移除的人员");
          return;
        }
        this.$Modal.confirm({
          title: "移除人员",
          content: `确定要从权限组中移除选中的人员么？`,
          onOk: () => {
            this.$axios({
              url: `/group/user/group/remove/${this.groupId}`,
              method: "POST",
              data: this.userList.map((a) => {
                return a.uid;
              }),
            }).then((res) => {
              if (res.code == 200) {
                this.getUser();
              } else {
                this.$Message.error(res.msg);
              }
            });
          },
        });
      },
      saveInfo() {
        if (this.deviceList.length == 0) {
          this.$Message.error("请先选择设备");
          return;
        }
        this.$axios({
          url: `/group/device/${this.groupId}`,
          method: "PUT",
          data: this.deviceList.map((a) => {
            return a.deviceid;
          }),
        }).then((res) => {
          if (res.code == 200) {
            this.showDevice = false;
            this.getUser();
          } else {
            this.$Message.error(res.msg);
          }
        });
      },
      pageChange(page) {
        this.page = page;
        this.getUser();
      },
      pageSizeChange(pageSize) {
        this.pageSize = pageSize;
        this.getUser();
      },
      getUser() {
        this.$axios({
          url: `/group/user/group/in/${this.groupId}`,
          method: "POST",
          data: {
            page: this.page,
            pageSize: this.pageSize,
            searchKey: this.searchItem.searchKey,
          },
        }).then((res) => {
          if (res.code == 200) {
            this.list = res.data.list;
            this.total = res.data.total;
            this.deviceList = [];
          } else {
            this.$Message.error(res.msg);
          }
        });
      },
  
      getData() {
        this.$axios({
          url: `/group/list`,
          method: "POST",
          data: {
            page: 1,
            pageSize: 10,
            searchKey: "",
          },
        }).then((res) => {
          if (res.code == 200) {
            this.datalist = res.data.list;
            if (this.datalist && this.datalist.length > 0) {
              setTimeout(() => {
                this.groupId = this.datalist[0].id;
                this.getUser();
              }, 200);
            }
          }
        });
      },
      deleteInfo(index) {
        var that = this;
        var model = this.datalist[index];
        this.$Modal.confirm({
          title: "删除提示",
          content: `确定要删除分组：“${model.title}”么？`,
          onOk: () => {
            this.$axios({
              url: `/group/${model.id}`,
              method: "DELETE",
            }).then((res) => {
              if (res.code == 200) {
                this.$Message.info(res.data);
                that.getData();
              } else {
                this.$Message.error(res.msg);
              }
            });
          },
        });
      },
    },
    created() {
      this.getData();
    },
  };
  